{extend name='index@template/base'}{/extend}
{block name='style'}

{/block}
{block name='script'}

{/block}
{block name='content'}
<div class="row">
    <div class="col-xs-12">
        <div class="box box-primary">
            <div class="box-body">
                <div id="toolbar">
                    <div class="btn-group pull-left mr10">
                        <a class="btn btn-primary" id="add_btn"><i class="fa fa-plus"></i> 新增</a>
                    </div>
                </div>
                <table id="table"
                       data-toggle="table"
                       data-show-refresh="true"
                       data-side-pagination="server"
                       data-pagination="true"
                       data-url="{:url('wechat/WechatTemplateMessage/index_data')}"
                >
                    <thead>
                    <tr>
                        <th data-field="id">编号</th>
                        <th data-field="title">模板标题</th>
                        <th data-field="template_short_id">模板编号</th>
                        <th data-field="template_id">模板ID</th>
                        <th data-field="status" data-formatter="formatStatus">是否启用</th>
                        <th data-field="operate" data-formatter="formatOperate" data-events="operateEvents" >操作</th>
                    </tr>
                    </thead>
                </table>
            </div><!-- /.box-body -->
        </div><!-- /.box -->
    </div><!-- /.col -->
</div><!-- /.row -->

{/block}
{block name='script_extra'}
<script>
    var table=$('#table');
    var add_data_url="{:url('wechat/wechatTemplateMessage/add')}";
    var edit_data_url="{:url('wechat/wechatTemplateMessage/edit')}";
    var delete_data_url="{:url('wechat/wechatTemplateMessage/delete')}";
</script>
<script>
    //格式化掉 末尾 .html
    function urlFormat(url) {
        if (url.indexOf('.html'))
        {
            url = url.replace('.html', '');
        }
        return url;
    }
    // <editor-fold defaultstate="collapsed" desc="添加">
    $('#add_btn').on('click',function(){
        layer_open('新增模板消息',add_data_url)
    });
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="操作">
    function formatOperate(value, row, index) {
        return [
            '<button class="btn btn-warning btn-sm row_edit" href="javascript:void(0)">',
            '修改',
            '</button>&nbsp;',
            '<button class="btn btn-danger btn-sm row_delete" href="javascript:void(0)">',
            ' 删除',
            '</button>'
        ].join('');
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="操作函数">
    window.operateEvents = {
        'click .row_edit': function (e, value, row, index) {
            layer_open('编辑',urlFormat(edit_data_url)+"/id/"+row.id);
        },
        'click .row_delete': function (e, value, row, index) {
            layer.confirm('你确定要删除该选项吗？', {btn: ['确定', '取消']}, function () {
                $.ajax({
                    dataType: 'json',
                    data: {id: row.id},
                    url: delete_data_url,
                    success: function (res) {
                        if (res.success) {
                            layer.msg(res.message);
                            $('#table').bootstrapTable('refresh');
                        } else {
                            layer.alert(res.message, {icon: 5})
                        }
                    }
                })
            })
        }
    };
    function formatStatus(value,row,index){
        if(value==1){
            return '<button onclick="changeStatus('+index+','+row.id+','+row.status+')" class="btn btn-success btn-xs" href="javascript:void(0)"><i class="fa fa-check-circle"></i> 启用</button>'
        }else{
            return '<button onclick="changeStatus('+index+','+row.id+','+row.status+')" class="btn btn-warning btn-xs" href="javascript:void(0)"><i class="fa fa-circle-o"></i> 禁用</button>'
        }
    }

    function changeStatus(index,id,status){
        var data={};
        if(status==1){
            data={id:id,status:0};
            $.ajax({
                dataType:'json',
                data:data,
                url:"{:url('wechat/WechatTemplateMessage/changeStatus')}",
                success:function(res){
                    table.bootstrapTable('updateRow',{index:index,row:{status:0}});
                }
            })
        }else{
            data={id:id,status:1}
            $.ajax({
                dataType:'json',
                data:data,
                url:"{:url('wechat/WechatTemplateMessage/changeStatus')}",
                success:function(res){
                    table.bootstrapTable('updateRow',{index:index,row:{status:1}});
                }
            })
        }

    }
</script>
{/block}